package hms.inventory;

import hms.database.DAO;
import hms.master.MasterController;

import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.DefaultListModel;

/**
 * InvColors is a collection of inventory colors
 * 
 * 
 * @author Akilah McIntyre
 * 
 */
public class InvColors extends DefaultListModel {
	public InvColor invColor;

	/**
	 * // TODO [Akilah] Add Comments
	 */
	public InvColors() {
	}

	/**
	 * Initialize colors from database
	 */
	public void initColors() {
		int color_id = -1; // unique identifier
		String color_name = ""; // name
		// get data from database associated with dao
		String size_count = "select count(*) from color where color_id =99 and color_NAME='UNDEFINED'";
        int undfCount = 0;
        try {
        	ResultSet rsCount = MasterController.getDatabaseObject().getResult(size_count);
        	while(rsCount.next()){
        		undfCount = rsCount.getInt(1);
        		if(undfCount == 0){
        			MasterController.getDatabaseObject().updateCommand("INSERT INTO COLOR(COLOR_ID,COLOR_NAME) VALUES(99,'UNDEFINED')");
        		}
        	}
    		// get data from database associated with dao
    		ResultSet rsColors = MasterController.getDatabaseObject().getResult(
    				"SELECT COLOR_ID, COLOR_NAME FROM COLOR WHERE COLOR_ID != 99 ORDER BY COLOR_ID");
		
			// Create InvColor instance from database
			while (rsColors.next()) {
				color_id = rsColors.getInt("COLOR_ID");
				color_name = rsColors.getString("COLOR_NAME");
				InvColor color_item = new InvColor(color_id, color_name);
				this.addElement(color_item); // add to colors model
			}
		} catch (SQLException e) {
			// TODO [Akilah] Write to view or popup dialog
			e.printStackTrace();
		}
	}

	/**
	 * Adds inventory color
	 * 
	 * @param next_color_id
	 *            unique identifier
	 * @param new_color_name
	 *            name of new color
	 */
	public void addColor(int next_color_id, String new_color_name) {
		this.addElement(new InvColor(next_color_id, new_color_name));
	}

	/**
	 * Adds inventory color
	 * 
	 * @param new_color_name
	 *            name of new color
	 * @return true if database insertion is success
	 * 
	 */
	public boolean addDbColor(String new_color_name) {
		boolean isSuccess = false;
		int next_color_id = -1;
		try {
			// Get new id as new id may not be +1
			next_color_id = MasterController.getDatabaseObject()
					.addDBTableValueParam("COLOR", "COLOR_NAME", "COLOR_ID",
							new_color_name);
			if (next_color_id > -1) { // if the insertion was successful
				this.addElement(new InvColor(next_color_id, new_color_name));
				isSuccess = true;
			}
		} catch (Exception ex) {
			isSuccess = false;
		}
		return isSuccess;
	}

	/**
	 * @param color_name
	 *            New Name of Color
	 * @param selIndex
	 *            Index of target color
	 * @return if database update is success
	 */
	public boolean updateDbColor(String color_name, int selIndex) {
		boolean isSuccess = false;
		int color_id = -1;
		try {
			color_id = ((InvColor) this.get(selIndex)).color_id;
			isSuccess = MasterController.getDatabaseObject()
					.updateDBTableValueParam("COLOR", "COLOR_NAME", "COLOR_ID",
							color_name, color_id);
			if (isSuccess) {
				this.changeInvColorName(color_name, selIndex);
			}
		} catch (Exception ex) {
			isSuccess = false;
		}
		return isSuccess;
	}

	/**
	 * @param color_name
	 *            New Name of Color
	 * @param selIndex
	 *            Index of target color
	 */
	public void changeInvColorName(String color_name, int selIndex) {
		InvColor destInvColor = ((InvColor) this.get(selIndex));
		destInvColor.color_name = color_name;
	}

	/**
	 * @param color_name
	 *            New Name of Color
	 * @param selIndex
	 *            Index of target color
	 */
	public boolean deleteDbColor(String color_name, int selIndex) {
		boolean isSuccess = false;
		int color_id = -1;
		try {
			color_id = ((InvColor) this.get(selIndex)).color_id;
			MasterController.getDatabaseObject().updateCommand("UPDATE INVENTORY SET COLOR_ID = 99 WHERE COLOR_ID = " + color_id);
			isSuccess = MasterController.getDatabaseObject()
					.updateCommand("DELETE FROM COLOR WHERE COLOR_ID = " + color_id);
			if (isSuccess) {
				this.deleteInvColorName(selIndex);
			}
		} catch (Exception ex) {
			isSuccess = false;
		}
		return isSuccess;
	}

	/**
	 * @param selIndex
	 *            Index of target color
	 */
	public void deleteInvColorName(int selIndex) {
		this.remove(selIndex);
		
	}
}